package liuyuyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import liuyuyang.domain.UserRole;
import liuyuyang.domain.User;
import liuyuyang.mapper.RoleMapper;
import liuyuyang.mapper.UserRoleMapper;
import liuyuyang.mapper.UserMapper;
import liuyuyang.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<User> list() {
        // 查询所有用户
        List<User> list = userMapper.selectList(null);

        for (User user : list) {
            // 根据ID找到每个用户所对应的权限
            QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("user_id", user.getUid());
            List<UserRole> userRoleList = userRoleMapper.selectList(queryWrapper);

            // 关联权限
            for (UserRole userRole : userRoleList) {
                user.getRole().add(roleMapper.selectById(userRole.getRoleId()));
            }
        }

        return list;
    }
}